package com.haowang.camera.activity;

import com.haowang.camera.R;
import com.haowang.camera.module.CameraModule;
import com.haowang.camera.module.PhotoModule;
import com.haowang.camera.module.VideoModule;
import com.haowang.camera.util.Util;

import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

public class CameraActivity extends Activity {

	private static final String TAG = CameraActivity.class.getSimpleName();
	private CameraModule mCameraModule;
	private FrameLayout mMainFrame;

	public CameraModule getCameraModule() {
		return mCameraModule;
	}

	public FrameLayout getMainFrame() {
		return mMainFrame;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_camera);
		init();
		mCameraModule.onCreate();
	}

	@Override
	protected void onResume() {
		mCameraModule.onResume();
		super.onResume();
	}

	@Override
	public void onBackPressed() {
		mCameraModule.onBackPressed();
		super.onBackPressed();
	}

	@Override
	protected void onPause() {
		mCameraModule.onPause();
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		mCameraModule.onDestroy();
		super.onDestroy();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.camera, menu);
		return true;
	}

	private void init() {
		Util.init(this);
		initView();
		if (MediaStore.INTENT_ACTION_VIDEO_CAMERA.equals(getIntent()
				.getAction())
				|| MediaStore.ACTION_VIDEO_CAPTURE.equals(getIntent()
						.getAction())) {
			mCameraModule = new VideoModule(this);
		} else {
			mCameraModule = new PhotoModule(this);
		}
		mCameraModule.init();
	}

	private void initView() {
		mMainFrame = (FrameLayout) findViewById(R.id.main_content);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		mCameraModule.getGestureManager().getGestureDetector()
				.onTouchEvent(event);
		return super.onTouchEvent(event);
	}
}
